//
//  Picker.m
//  DemoRongCloud
//
//  Created by yuanweitiansheng on 16/8/17.
//  Copyright © 2016年 louyizhidu. All rights reserved.
//

#import "Picker.h"
#import "Province.h"
#import "City.h"
@interface Picker ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    NSMutableDictionary* provinces;
    /**
     * 
     第一列选择的数据
     两列之间的关联数据
     */
    NSString* selected;
}
@end

@implementation Picker

- (void)viewDidLoad
{
    [super viewDidLoad];
    provinces=[[NSMutableDictionary alloc] init];
    NSString* path=[[NSBundle mainBundle] pathForResource:@"Provineces"
                                                   ofType:@"plist"];
   NSArray* provineces=[NSArray arrayWithContentsOfFile:path];
    [provineces enumerateObjectsUsingBlock:^(id  _Nonnull obj,
                                         NSUInteger idx,
                                         BOOL * _Nonnull stop)
     {
         NSDictionary* provinceData=obj;
         /**
          构建省份数据
          */
         Province* province=[[Province alloc] init];
         province.provinceName=provinceData[@"ProvinceName"];
         NSArray* cities=provinceData[@"cities"];
         NSMutableArray* citys=[[NSMutableArray alloc] init];
         [cities enumerateObjectsUsingBlock:^(id  _Nonnull obj,
                                              NSUInteger idx,
                                              BOOL * _Nonnull stop)
          {
              NSDictionary* cityData=obj;
              /**
               构建城市数据
               */
              City* city=[[City alloc] init];
              city.cityName=cityData[@"CityName"];
              [citys addObject:city];
         }];
         /**
          *  城市数据和省市数据的联动
          */
         province.cities=citys;
         [provinces setObject:province
                       forKey:province.provinceName];
    }];
    selected=[(Province*)[provinces objectForKey:provinces.allKeys[0]] provinceName];
}
- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    return [[[NSBundle mainBundle] loadNibNamed:@"Picker"
                                          owner:self
                                        options:nil] firstObject];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - <UIPickerViewDataSource>
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
        {
            return provinces.allKeys.count;
        }
            break;
        case 1:
        {
            /**
             *  
             取出省份
             取出省份的城市数据
             */
            return [[(Province*)[provinces objectForKey:selected] cities] count];
        }
            break;
    
        default:
            break;
    }
    return 0;
    
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView
                      titleForRow:(NSInteger)row
                     forComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
        {
            return provinces.allKeys[row];
        }
            break;
        case 1:
        {
            NSArray* cities=[(Province*)[provinces objectForKey:selected] cities];
            
            return [(City*)cities[row] cityName];
        }
            break;
    
        default:
            break;
    }
    return nil;
}
-(void)pickerView:(UIPickerView *)pickerView
     didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
        {
            selected=provinces.allKeys[row];
            /**
             *  刷新指定列表的相关数据
             */
            [pickerView reloadComponent:1];
        }
            break;
        case 1:
        {
            
        }
            break;

        default:
            break;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
